
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyCalculation extends JFrame {
    private JButton Button;
    private JTextField text1, text2, text3, text4, text5, text6;
    public JComboBox<String> combox1, combox2, combox3, combox4;
    private static String[] signs = {"+", "-", "*", "/", "%"};
    public JDialog alarm;

    public MyCalculation() {
        super("算术运算器");
        this.setBounds(500, 500, 400, 150);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        this.add(text1 = new JTextField(10));
        this.add(combox1 = new JComboBox<String>(signs));
        this.add(text2 = new JTextField(10));
        this.add(combox2 = new JComboBox<String>(signs));
        this.add(text3 = new JTextField(10));
        this.add(combox3 = new JComboBox<String>(signs));
        this.add(text4 = new JTextField(10));
        this.add(combox4 = new JComboBox<String>(signs));
        this.add(text5 = new JTextField(10));
        this.add(Button = new JButton("="));
        this.add(text6 = new JTextField(10));
        this.text6.setEditable(false);
        this.setVisible(true);

        ActionListener mylistener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double[] a = {Double.valueOf(text1.getText()),
                        Double.valueOf(text2.getText()),
                        Double.valueOf(text3.getText()),
                        Double.valueOf(text4.getText()),
                        Double.valueOf(text5.getText())};

                int[] b = {combox1.getSelectedIndex(),
                        combox2.getSelectedIndex(),
                        combox3.getSelectedIndex(),
                        combox4.getSelectedIndex()};
                double c = 0.0, temp = 0.0;

                if ((b[0] == 3 && a[1] == 0) || (b[0] == 4 && a[1] == 0) ||
                        (b[1] == 3 && a[2] == 0) || (b[1] == 4 && a[2] == 0) ||
                        (b[2] == 3 && a[3] == 0) || (b[2] == 4 && a[3] == 0) ||
                        (b[3] == 3 && a[4] == 0) || (b[3] == 4 && a[4] == 0))
                    JOptionPane.showMessageDialog(alarm, "被除数不能为零，请重新输入！");
                try {
                      temp  = a[0];
                    for (int m = 1; m < 5; m++) {
                        if (b[m-1] == 0)
                            temp+=a[m];
                        else if (b[m-1]==1)
                            temp-=a[m];
                        else if (b[m-1]==2)
                            temp*=a[m];
                        else if (b[m-1]==3)
                            temp/=a[m];
                        else
                            temp%=a[m];
                    }
                    c=temp;
                } catch (NumberFormatException event) {
                }
                text6.setText("" + c);
            }
        };
        this.Button.addActionListener(mylistener);
    }
    public static void main(String[] args) {
        new MyCalculation();
    }
}
